using IcsSEMCtrl.Event;
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;
namespace IcsSEMCtrl.Service.EventBroker
{
	public class EventManagerTestClient : MarshalByRefObject
	{
		public RemoteEvent remoteEvent = null;
		public WrapperRemoteEvent wrapperRemoteEvent = null;
		public void Init()
		{
			TcpChannel chnl = new TcpChannel();
			ChannelServices.RegisterChannel(chnl, false);
			this.remoteEvent = (RemoteEvent)Activator.GetObject(typeof(RemoteEvent), "tcp://localhost:9000/RemoteEvent");
		}
		public void InitByConfigfile()
		{
			RemotingConfiguration.Configure(Environment.CurrentDirectory + "\\TestClient.exe.config", false);
		}
		public void Subscribe()
		{
			this.remoteEvent = (RemoteEvent)Activator.GetObject(typeof(RemoteEvent), "tcp://localhost:9000/RemoteEvent");
			this.wrapperRemoteEvent = new WrapperRemoteEvent();
			try
			{
				this.remoteEvent.RemoteEventReceived += new RemoteEventHandler(this.WapperRemoteEvent_WrapperRemoteEventReceived);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message.ToString());
				Console.WriteLine(ex.StackTrace);
			}
		}
		public void Unsubscribe()
		{
			try
			{
				this.remoteEvent.RemoteEventReceived -= new RemoteEventHandler(this.wrapperRemoteEvent.WrapperRemoteEventReceivedHandler);
				this.wrapperRemoteEvent.WrapperRemoteEventReceived -= new RemoteEventHandler(this.WapperRemoteEvent_WrapperRemoteEventReceived);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message.ToString());
			}
		}
		public void WapperRemoteEvent_WrapperRemoteEventReceived(object eventData)
		{
		}
		public void Test()
		{
			this.InitByConfigfile();
			this.Subscribe();
			int num = 0;
			while (true)
			{
				num++;
				this.remoteEvent.Publish(new ECPEvent(Convert.ToString(num)));
				Thread.Sleep(100);
				Console.ReadLine();
			}
		}
	}
}
